<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			*{
				padding: 0%;
				margin: 0%;
			}
			#box{
				background-color: red;
				width: 200px;
				height: 200px;
				position: relative;
				left: -200px;
			}
			#box span {
				width: 40px;
				height: 60px;
				position: absolute;
				right: -40px;
				top: 50%;
				margin-top: -30px;
				line-height: 60px;
				text-align: center;
				background-color: black;
				color: #fff;
				
			}
		</style>
	</head>
	<body>
		<div id="box">
			<span>拉开</span>
		</div>
	</body>
	<script>
		//加速度 = (结束值 - 初始值) / 缓动系数
		window.onload = function() {
			let box = document.getElementById('box');
			let timer = null,speed = 0,end1 = 0,end2 = -200;
			box.onmouseover = function() {
				slowAnimation(this,end1);
			}
			box.onmouseout = function() {
				slowAnimation(this,end2);
			}
			function slowAnimation(obj,end) {
				clearInterval(timer);
				timer = setInterval(function(){
					speed = end >= 0 ? Math.ceil((end - box.offsetLeft) / 20) : Math.floor((end - box.offsetLeft) / 20);
					if(box.offsetLeft === end) {
						clearInterval(timer);
						return;
					}
					box.style.left = box.offsetLeft + speed + 'px';
				},30);
			}
		}
	</script>
</html>